﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Otonomos.World;

namespace Test.Otonomos.World
{
    [TestClass]
    public class TestPoint3D
    {
        [TestMethod]
        public void CreatePoint()
        {
            Point3D point = new Point3D(1.0, 2.0, 3.0);
            Assert.AreEqual(point.X, 1.0);
            Assert.AreEqual(point.Y, 2.0);
            Assert.AreEqual(point.Z, 3.0);
        }

        [TestMethod]
        public void ComparePoints()
        {
            var point = new Point3D();
            Assert.AreEqual(point, Point3D.Origin);

            var point1 = new Point3D(1.0, 2.0, 3.0);
            var point2 = new Point3D(1.0, 2.0, 3.0);
            var point3 = new Point3D(1.0, 2.1, 3.0);
            Assert.AreEqual(point1, point2);
            Assert.AreEqual(point2, point1);
            Assert.IsTrue(point1 == point2);
            Assert.IsTrue(point2 == point1);
            Assert.AreNotEqual(point3, point);
            Assert.AreNotEqual(point3, point1);
            Assert.AreNotEqual(point3, point2);
            Assert.IsFalse(point1 == null);
            Assert.IsFalse(null == point3);
            Assert.IsFalse(point2.Equals(null));
        }

        [TestMethod]
        public void SortPoints()
        {
            var point1 = new Point3D(1.0, 2.0, 3.0);
            var point2 = new Point3D(2.0, 1.0, 3.0);
            var point3 = new Point3D(2.0, 2.0, 3.0);
            var point4 = new Point3D(2.0, 2.0, 4.0);
            var point5 = new Point3D(1.0, 1.0, 1.0);
            
            var points = new List<Point3D>();
            points.Add(point2);
            points.Add(point4);
            points.Add(point5);
            points.Add(point1);
            points.Add(point3);
            points.Sort();

            Assert.AreSame(points[0], point5);
            Assert.AreSame(points[1], point1);
            Assert.AreSame(points[2], point2);
            Assert.AreSame(points[3], point3);
            Assert.AreEqual(points[4], point4);
        }

        [TestMethod]
        public void Distance()
        {
            Random rand = new Random();
            var point1 = new Point3D(rand.NextDouble(), rand.NextDouble(), rand.NextDouble());
            var point2 = new Point3D(rand.NextDouble(), rand.NextDouble(), rand.NextDouble());

            Assert.AreEqual(point1.Distance(point2), point2.Distance(point1));
            Assert.AreEqual(point1.Distance(point1), 0.0);
            Assert.AreEqual(point2.Distance(point2), 0.0);

            var point3 = new Point3D(1.0, 2.0, 3.0);
            Assert.AreEqual(point3.Distance(Point3D.Origin), Math.Sqrt(14.0));
        }
    }
}
